package com.hx.er.action.admin.settings;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.base.CityBean;
import com.hx.er.bean.detail.DistrictDetailBean;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.admin.IAdminSettingsService;

public class AdminDistrictAddAction extends ErAdminAction {
	// 序列化ID
	private static final long serialVersionUID = -1127474451530316813L;

	// 页面消息
	public static final String DISTRICT_NAME_UNIQUE_MSG = "admin.districtList.message.add.unique.districtName";
	public static final String DISTRICT_NAME_EMPTY_MSG = "admin.districtList.message.add.empty.districtName";
	public static final String DISTRICT_NAME_NOT_LETTERS = "admin.districtList.message.add.letters.districtName";
	public static final String LOCAL_DISTRICT_NAME_EMPTY_MSG = "admin.districtList.message.add.empty.localDistrictName";

	private long cityId;
	private DistrictDetailBean districtDetailBean;
	private String updateDateStr;

	@Autowired
	private IAdminSettingsService adminSettingsService;

	public String init() {
		this.districtDetailBean = new DistrictDetailBean();
		
		CityBean cityBean = this.adminSettingsService.getCityById(this.cityId);
		this.districtDetailBean.setCityId(cityBean.getCityId());
		this.districtDetailBean.setCityName(cityBean.getCityName());
		this.districtDetailBean.setLocalCityName(cityBean.getLocalCityName());
		
		return SUCCESS;
	}

	public String save() {
		if (!this.checkParams()) {
			return INPUT;
		}

		this.districtDetailBean.setCityId(this.cityId);
		this.districtDetailBean.setCreateUserId(super.getUserInfoLogin().getUserId());
		this.districtDetailBean.setUpdateUserId(super.getUserInfoLogin().getUserId());

		// 插入数据
		boolean successFlag = this.adminSettingsService.insertDistrictWithoutId(this.districtDetailBean);

		// 根据插入结果返回
		if (successFlag) {
			this.addActionMessage(super.getText(ADD_SUCCESS_MSG));
			return SUCCESS;
		} else {
			this.addFieldError("msg.districtName", super.getText(DISTRICT_NAME_UNIQUE_MSG));
			return INPUT;
		}
	}

	private boolean checkParams() {
		// 城市英文名验证
		if (StringUtil.isEmpty(this.districtDetailBean.getDistrictName())) {
			this.addFieldError("msg.districtName", super.getText(DISTRICT_NAME_EMPTY_MSG));
		} else if (!StringUtil.isCharsLetter(this.districtDetailBean.getDistrictName())) {
			this.addFieldError("msg.districtName", super.getText(DISTRICT_NAME_NOT_LETTERS));
		}

		// 城市本地名验证
		if (StringUtil.isEmpty(this.districtDetailBean.getLocalDistrictName())) {
			this.addFieldError("msg.localDistrictName", super.getText(LOCAL_DISTRICT_NAME_EMPTY_MSG));
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	public long getCityId() {
		return cityId;
	}

	public void setCityId(long cityId) {
		this.cityId = cityId;
	}

	public String getUpdateDateStr() {
		return updateDateStr;
	}

	public void setUpdateDateStr(String updateDateStr) {
		this.updateDateStr = updateDateStr;
	}

	public DistrictDetailBean getDistrictDetailBean() {
		return districtDetailBean;
	}

	public void setDistrictDetailBean(DistrictDetailBean districtDetailBean) {
		this.districtDetailBean = districtDetailBean;
	}

}
